package com.ayu.commentbackend.service;

import com.ayu.commentbackend.StatusConstant;
import com.ayu.commentbackend.utils.DeepSeekUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class AIService {
    @Autowired
    BlogService blogService;

    @Async("aiTaskExecutor")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void deepSeek(String contents, Long id){
        try {
            String aiResult = DeepSeekUtil.chat(contents);
            blogService.updateOtherEventWithAiStatus(aiResult, StatusConstant.AI_SUCCESS, id);
        } catch (Exception e) {
            log.error("调用DeepSeek时出现错误 ID:{}", id, e);
            blogService.updateAiStatus(StatusConstant.AI_FAILED, id);
        }
    }
}
